package org.ITschool.support.util;

import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MybatisTempletSupport {
	ThreadLocal<SqlSession> session=new ThreadLocal<SqlSession>();
	private static ApplicationContext context;
	private void setsession(SqlSession session1){
		session.set(session1);
	}
	private SqlSession getSession(){
		return session.get();
	}
    public SqlSession getSqlSession(){
    	try {
			getSession().getConnection().isClosed();
		} catch (Exception e) {
			// TODO: handle exception
			SessionFactory sessionFactory=new SessionFactory();
			setsession(sessionFactory.openSession());
		}
    	return getSession();
    }
    public SqlSession openSession(){
    	context=new ClassPathXmlApplicationContext("SpringITschool-base.xml");
    	return (SqlSession)context.getBean("sqlSession");
    }
}
